<template>
  <div class="login-container" @keyup.enter.native="handleLogin">
    <top-color v-show="false"></top-color>
    <div class="login-weaper animated bounceInDown">
      <div class="login-left">
        <img :src="logo" class="el-image__inner" />
      </div>
      <div class="login-border">
        <div class="login-main">
          <h4 class="login-title">
            <!--{{ $t('login.title') }}{{website.title}}
            <top-lang></top-lang>-->
            登录
          </h4>
          <userLogin v-if="activeName === 'user'"></userLogin>
          <codeLogin v-else-if="activeName === 'code'"></codeLogin>
          <thirdLogin v-else-if="activeName === 'third'"></thirdLogin>
          <div class="login-menu">
            <!--<router-link :to="{ path: '/password' }"
              ><a href="#">忘记密码</a></router-link
            >-->
            <a href="#" @click.stop="activeName = 'user'">{{
              $t("login.userLogin")
            }}</a>
            <a href="#" @click.stop="activeName = 'code'">{{
              $t("login.phoneLogin")
            }}</a>
            <!--<a href="#" @click.stop="activeName='third'">{{ $t('login.thirdLogin') }}</a>-->
            <!--<router-link :to="{ path: '/password' }"
              ><a href="#">修改密码</a></router-link
            >-->
          </div>
        </div>
      </div>
    </div>
    <div class="xxxx">
      <div><a href="https://beian.miit.gov.cn">粤ICP备2021097590号</a></div>
      <div>深圳市鸿康心理辅导有限公司</div>
    </div>
  </div>
</template>
<script>
import userLogin from "./userlogin";
import codeLogin from "./codelogin";
import thirdLogin from "./thirdlogin";
import { mapGetters } from "vuex";
import { dateFormat } from "@/util/date";
import { validatenull } from "@/util/validate";
import topLang from "@/page/index/top/top-lang";
import topColor from "@/page/index/top/top-color";
import { getQueryString, getTopUrl } from "@/util/util";

import logo from "../img/logo.png";

export default {
  name: "login",
  components: {
    userLogin,
    codeLogin,
    thirdLogin,
    topLang,
    topColor,
  },
  data() {
    return {
      logo,
      time: "",
      activeName: "user",
      socialForm: {
        tenantId: "000000",
        source: "",
        code: "",
        state: "",
      },
    };
  },
  watch: {
    $route() {
      this.handleLogin();
    },
  },
  created() {
    this.handleLogin();
    this.getTime();
  },
  mounted() {
    // window.alert("测试自动发包");
  },
  computed: {
    ...mapGetters(["website", "tagWel"]),
  },
  props: [],
  methods: {
    getTime() {
      setInterval(() => {
        this.time = dateFormat(new Date());
      }, 1000);
    },
    handleLogin() {
      const topUrl = getTopUrl();
      const redirectUrl = "/oauth/redirect/";
      this.socialForm.source = getQueryString("source");
      this.socialForm.code = getQueryString("code");
      this.socialForm.state = getQueryString("state");
      if (
        validatenull(this.socialForm.source) &&
        topUrl.includes(redirectUrl)
      ) {
        let source = topUrl.split("?")[0];
        source = source.split(redirectUrl)[1];
        this.socialForm.source = source;
      }
      if (
        !validatenull(this.socialForm.source) &&
        !validatenull(this.socialForm.code) &&
        !validatenull(this.socialForm.state)
      ) {
        const loading = this.$loading({
          lock: true,
          text: "第三方系统登录中,请稍后。。。",
          spinner: "el-icon-loading",
        });
        this.$store
          .dispatch("LoginBySocial", this.socialForm)
          .then(() => {
            window.location.href = topUrl.split(redirectUrl)[0];
            this.$router.push({ path: this.tagWel.value });
            loading.close();
          })
          .catch(() => {
            loading.close();
          });
      }
    },
    open() {
      this.$alert(
        "<strong>这是 <i>HTML</i> 片段</strong>",
        "请选择学校或机构",
        {
          dangerouslyUseHTMLString: true,
          showClose: false,
          showCancelButton: false,
          showConfirmButton: false,
        }
      );
    },
  },
};
</script>

<style lang="scss">
@import "@/styles/login.scss";
.login-container {
  background: url("../img/cloud.jpg") 0 bottom repeat-x #049ec4;
}

.xxxx {
  width: 200px;
  text-align: center;
  position: absolute;
  top: 90%;
  left: 50%;
  margin-left: -100px;
  z-index: 9999;
  font-size: 14px;

  a {
    color: rgb(3, 135, 207);
  }
}
</style>
